package cn.suzhang.jsharp.module.sys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.suzhang.jsharp.common.entity.JsonEntity;
import cn.suzhang.jsharp.module.sys.entity.SysRole;
import cn.suzhang.jsharp.module.sys.entity.SysUser;
import cn.suzhang.jsharp.module.sys.service.RoleService;
import cn.suzhang.jsharp.module.sys.service.UserService;

/**
 * 
 * @author suzhang
 *
 */
@Controller
@RequestMapping("/admin/user")
public class UserController {

	@Autowired
	private UserService userService;
	
	@Autowired
	private RoleService roleService;

	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String getUserList(Model model) {
		List<SysUser> list = userService.findAll();
		model.addAttribute("list", list);
		return "/admin/user/user";
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add(Model model) {
		return "/admin/user/form";
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable Long id, Model model) {
		SysUser user = userService.find(id);
		model.addAttribute("user", user);
		return "/admin/user/form";
	}

	@RequestMapping(value = "/find", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> findSearch(SysUser sysUser) {
		Map<String, Object> map = new HashMap<>();
		List<SysUser> list = userService.findSearch(sysUser);
		map.put("recordsTotal", list.size());
		map.put("data", list);
		return map;
	}
	
	 @RequestMapping(value = {"/edit"}, method = RequestMethod.POST)
	    @ResponseBody
	    public JsonEntity edit(SysUser user, Model model) {
	        try {
	            userService.saveOrUpdate(user);
	        } catch (Exception e) {
	            return JsonEntity.failure(e.getMessage());
	        }
	        return JsonEntity.success();
	    }
	 
	 @RequestMapping(value = "/grant/{id}", method = RequestMethod.GET)
	    public String grant(@PathVariable Long id, Model model) {
	        SysUser user = userService.find(id);
	        model.addAttribute("user", user);

	        Set<SysRole> set = user.getRoles();
	        List<Long> roleIds = new ArrayList<Long>();
	        for (SysRole role : set) {
	            roleIds.add(role.getId());
	        }
	        model.addAttribute("roleIds", roleIds);

	        List<SysRole> roles = roleService.findAll();
	        model.addAttribute("list", roles);
	        return "admin/user/grant";
	    }

	    @ResponseBody
	    @RequestMapping(value = "/grant/{id}", method = RequestMethod.POST)
	    public JsonEntity grant(@PathVariable Long id, String[] roleIds, ModelMap map) {
	        try {
	            userService.grant(id, roleIds);
	        } catch (Exception e) {
	            e.printStackTrace();
	            return JsonEntity.failure(e.getMessage());
	        }
	        return JsonEntity.success();
	    }


}
